import dt from '@dt/dt'
import ruleOpt from '@/biz/option/rule.js'
import toolMgr from '@/biz/base/tool.js'
import housetypeMgr from '@/biz/housetype/housetype'

let _this = null
let selectList = [
{
  key: 0,
  value: '0',
},
{
  key: 1,
  value: '1',
},
{
  key: 2,
  value: '2',
},
{
  key: 3,
  value: '3',
},
{
  key: 4,
  value: '4',
},
{
  key: 5,
  value: '5',
},
{
  key: 6,
  value: '6',
},
{
  key: 7,
  value: '7',
},
{
  key: 8,
  value: '8',
},
{
  key: 9,
  value: '9',
},
{
  key: 10,
  value: '10',
}]

function rulesPattern(msg, ok) {
  let validator = (rule, value, callback) => {
    console.log(value, '&&&&')
    if (ok(value)) {
      return callback(msg)
    }
    callback()
  }
  return {
    validator,
    trigger: 'blur,change'
  }
}

function clearNoNum(val) {
  return String(val).replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$')
    .replace(/\./g,
      '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
}


function pattern() {
  let opt = {
    title: '格局',
    key: 'bedroom',
    group: [{
      key: 'bedroom',
      col: 5,
      disabled: _this.is_view,
      select: {
        options: selectList,
      },
    }, {
      placeholder: '室',
      col: 1,
      text: {}
    }, {
      key: 'hall',
      col: 5,
      disabled: _this.is_view,
      select: {
        options: selectList,
      },
    }, {
      placeholder: '厅',
      col: 1,
      text: {}
    }, {
      key: 'toilet',
      col: 5,
      disabled: _this.is_view,
      select: {
        options: selectList,
      },
    }, {
      placeholder: '卫',
      col: 1,
      text: {}
    }],
    rules: [
      ruleOpt.required('请选择格局'),
      rulesPattern('请选择格局', (value) => {
        let flag = true
        flag = _this.info.bedroom == null || _this.info.bedroom == '' || _this.info
          .hall == null || _this
          .info.hall == '' ||
          _this.info.toilet == null || _this.info.toilet == ''
        console.log(flag, '格局格局格局')
        return flag
      })
    ]
  }

  return opt
}

function floorPic(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.houseTypeAnnex && info.houseTypeAnnex.length > 0) {
    info.houseTypeAnnex.forEach(item => {
      if (item) {
        files.push({
          name: item.name,
          url: item.url,
          cover: item.cover
        })
      }
    })
  }
  return {
    title: '户型图',
    key: 'houseTypeAnnex',
    uploadImg: {
      title: '户型图',
      api,
      files,
      multiple: false,
      hasCover: true,
      trueRadioTxt: '封面图',
      falseRadioTxt: '设置封面图',
      preview: (file) => {
        console.log(file)
        toolMgr.downloadFile(file.url, file.name)
      },
      show: false,
      hideDel: _this.is_view ? true : false,
      maxlength: _this.is_view ? -1 : 30,
      format: ['jpg', 'jpeg', 'png'],
      formatErr: () => {
        _this.$Message.error('只支持上传jpg、jpeg、png图片！')
      },
    },
    data: {
      to: (v) => {
        let houseTypeAnnex = v.map(item => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            cover: item.cover ? item.cover : '2'
          }
        })
        return {
          houseTypeAnnex
        }
      }
    }
  }
}

function getHouseTypeTag() {
  let params = {
    cityCode: dt.buildInfo().cityId,
  }
  return housetypeMgr.houseTypeTags(params).then((res) => {
    console.log('所有标签----------------', res)
    res.houseTypeFeature && res.houseTypeFeature.forEach(item => {
      item.key = String(item.baseTagsId)
      item.value = item.baseTagsName
    })
    return res.houseTypeFeature
  })
}

function options(vm) {
  _this = vm
  let arr = [
    {
      title: '户型名称',
      placeholder: '请输入户型名称',
      key: 'houseTypeName',
      disabled: _this.is_view,
      col: 12,
      input: { maxlength: 20 },
      rules: [{
        required: true,
        message: '户型名称不能为空',
        trigger: 'blur'
      }]
    },
    pattern(),
    {
      title: '朝向',
      key: 'orientation',
      col: 12,
      disabled: _this.is_view,
      select: {
        options: dt.dictInfo().orientation,
      },
    },
    {
      title: '装修',
      key: 'decoration',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: dt.dictInfo().residenceDecoration,
      },
    },
    {
      title: '建面',
      key: 'floorAreaStart',
      col: 12,
      disabled: _this.is_view,
      input: {
        appendTxt: '㎡',
        blur: (val, option) => {
          _this.info[option.key] = clearNoNum(val)
        }
      }
    },
    {
      title: '单价',
      key: 'unitPriceStart',
      col: 12,
      disabled: _this.is_view,
      input: {
        appendTxt: '元/㎡',
        blur: (val, option) => {
          _this.info[option.key] = clearNoNum(val)
        }
      }
    },
    {
      title: '总价',
      key: 'totalPriceStart',
      col: 12,
      disabled: _this.is_view,
      input: {
        appendTxt: '万元',
        blur: (val, option) => {
          _this.info[option.key] = clearNoNum(val)
        }
      }
    },
    {
      title: '销售状态',
      key: 'salesStatus',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: dt.dictInfo().buSalesStatus,
      },
      rules: [{
        required: true,
        message: '销售状态不能为空',
        trigger: 'blur'
      }]
    },
    floorPic(_this.info),
    {
      title: '户型点评',
      key: 'houseTypeComment',
      col: 20,
      disabled: _this.is_view,
      input: {
        type: 'textarea',
        maxlength: 200,
        autosize: {
          minRows: 2,
          maxRows: 5
        }
      },
    },
    {
      title: '户型标签',
      key: 'houseTypeTag',
      col: 24,
      load: (o) => {
        getHouseTypeTag().then(res => {
          o.checkbox.options = res
        })
      },
      disabled: _this.is_view,
      checkbox: {
        options: []
      },
      change: (val) => {
        if (val.value.length > 3) {
          _this.$Message.error('户型标签至多只可选择3个')
          val.value.pop()
        }
      }
    }
  ]
  return arr
}

export default {
  options
}